﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XYTXL.AppCode;
using System.Data;
using XYTXL.Data;
using Newtonsoft.Json;
using System.IO;
using XYTXL.API;


namespace XYTXL.View
{
    public partial class CommonListPage : BasePage
    {
        public string strFormCode
        {
            get { return Request.QueryString["FormCode"] ?? "XTGG"; }
        }



        protected void Page_Load(object sender, EventArgs e)
        {
            JH_Auth_FormBase FormBaseData = new JH_Auth_FormBaseB().GetEntity(d => d.FormCode == strFormCode);
            string strQueryName = "/View/UCList/" + FormBaseData.FormQueryUrl;
            hidCRUser.Value = UserInfo.User.UserName;
            QueryUControl Control = new QueryUControl();
            QueryUControl Control1 = new QueryUControl();
            QueryUControl Control2 = new QueryUControl();
            if (!string.IsNullOrEmpty(FormBaseData.FormQueryUrl))
            {
                Control = LoadControl(strQueryName) as QueryUControl;
                QueryCon.Controls.Add(Control);
                Button btQuery = Control.FindControl("btQuery") as Button;
                if (btQuery != null)
                {
                    btQuery.Click += new EventHandler(this.btQuery_Click);
                }
                Button btExport = Control.FindControl("btExport") as Button;
                if (btExport != null)
                {
                    btExport.Click += new EventHandler(this.btExport_Click);
                }
            }
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(FormBaseData.FormQueryUrl))
                {
                    querydata(Control.SetinitQuery() + Control.SetDefaultQuery());
                }
                else
                {
                    querydata("");
                }
            }
        }


        public void querydata(string strWhere)
        {
            string strReturn = "";

            JH_Auth_FormBase FormBaseData = new JH_Auth_FormBaseB().GetEntity(d => d.FormCode == strFormCode);

            string strFormTable = FormBaseData.FormTable;
            if (!string.IsNullOrEmpty(FormBaseData.FormGetSQL))
            {
                strFormTable = " (" + FormBaseData.FormGetSQL + ") as Form ";
            }
       

            DataTable dtResult = new DataTable();

            

            dtResult = new JH_Auth_FormBaseB().GetDTByCommand(" select  ID,xh,xm,bj,xb,NJ,xbmc,zymc,sfzc,nzcname,now_smscode from  " + strFormTable + " where 1=1 " + strWhere + " ORDER BY ID DESC");
          
        
             
            var timeConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            strReturn = JsonConvert.SerializeObject(dtResult, timeConverter);
            tabledata.Value = strReturn;




        }
        void btQuery_Click(Object sender, EventArgs e)
        {
            // QueryUControl Control = QueryCon.FindControl("UserQueryControl") as QueryUControl;

            QueryUControl Control = QueryCon.Controls[0] as QueryUControl;
            string strQuery = Control.SetinitQuery() + Control.GetQuerydata();
            hidselID.Value = "";//初始化选择为空
            querydata(strQuery);
        }
        void btExport_Click(Object sender, EventArgs e)
        {
            QueryUControl Control = QueryCon.Controls[0] as QueryUControl;
            string strQuery = Control.SetinitQuery() + Control.GetQuerydata();
            hidselID.Value = "";//初始化选择为空 
            JH_Auth_FormBase FormBaseData = new JH_Auth_FormBaseB().GetEntity(d => d.FormCode == strFormCode);
            string strFormTable = FormBaseData.FormTable;
            if (!string.IsNullOrEmpty(FormBaseData.FormGetSQL))
            {
                strFormTable = " (" + FormBaseData.FormGetSQL + ") as Form ";
            }
            DataTable dtResult = new DataTable();
            dtResult = new JH_Auth_FormBaseB().GetDTByCommand(" select  * from  " + strFormTable + " where 1=1 " + strQuery + " ORDER BY ID DESC");

            string fileName = FormBaseData.FormDec + ".xls";
            MemoryStream ms = CommonHelp.RenderToExcel(dtResult);
            if (Request.Browser.Browser == "IE")
                fileName = HttpUtility.UrlEncode(fileName);
            Response.AddHeader("Content-Disposition", "attachment;fileName=" + fileName);
            Response.BinaryWrite(ms.ToArray());

        }

    }
}